﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using yq.ControllerBase;
using System.Web.Mvc;

namespace yq.Controller.File
{
    /// <summary>
    /// 私有
    /// </summary>
    public class HomeController : BaseController
    {
        /// <summary>
        /// File
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            Search.File.FileListSeek search = new Search.File.FileListSeek();
            search.SuperiorId = "-1";

            return View(search);
        }

        /// <summary>
        /// 打开文件夹
        /// </summary>
        /// <returns></returns>
        [ErrorFilter("打开文件夹失败", ErrorFilterType.ReturnValue)]
        public ActionResult SelectFlie(Search.File.FileListSeek search)
        {
            if (search == null)
            {
                search = new Search.File.FileListSeek();
            }
            TryUpdateModel(search);
            search.IsPrivate = Enums.BooleanType.No;
            search.Status = Enums.FileList.Status.Normal;
            search.UserId = GetCurrentUser.UserId;
            var list = new yq.WebModel.File.FileListPage();
            list.CurrFileList = yq.Model.Logic.FileList.Load(search.SuperiorId);
            list.List = yq.GetService.Create<yq.IService.IFile>().SelectFile(search);
            list.AddRoleLinkButton("设置", "EditFolder", "Home", "File");

            //操作
            list.AddRoleLinkButton("版本", "SelectFlieVer", "Home", "File");
            list.AddRoleLinkButton("强制签入", "MyCheckInFlie", "Home", "File");
            list.AddRoleLinkButton("锁定", "CheckInFlie", "Home", "File");
            list.AddRoleLinkButton("解锁", "CheckOutFlie", "Home", "File");
            list.AddRoleLinkButton("共享", "Index", "Purview", "File");

            list.AddRoleLinkButton("删除", "DoDelFlie", "Home", "File");

            list.AddRoleLinkButton("编辑", "EditFile", "Home", "File");
            list.AddRoleLinkButton("下载", "DownFile", "Home", "File");

            return PartialView(list);
        }


        /// <summary>
        /// 查看历史版本
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [VerifyFilter("FileinfoId", VerifyHelp.FileReadKey)]
        public ActionResult SelectFlieVer(string fileinfoId)
        {
            yq.WebModel.ListPage<Model.Logic.FileVer> list = new WebModel.ListPage<Model.Logic.FileVer>();
            list.List = yq.GetService.Create<yq.IService.IFile>().SelectFileVer(fileinfoId);
            list.AddRoleLinkButton("下载", "DownFile", "Home", "File");

            return PartialView(list);
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [VerifyFilter("VerId", VerifyHelp.FileReadKey)]
        public ActionResult DownFile()
        {
            var model = this.GetModel<yq.Model.Logic.FileVer>();
            yq.Controller.Home.PublicReuseController cm = new Home.PublicReuseController();

            return cm.DownFile(model.FileId);
        }

        /// <summary>
        /// 添加文件夹
        /// </summary>
        /// <returns></returns>
        public ActionResult AddFolder(string superiorId)
        {
            Model.Logic.FileList file = new Model.Logic.FileList();
            file.SuperiorId = superiorId;

            return PartialView(file);
        }

        /// <summary>
        /// 执行添加文件夹
        /// </summary>
        /// <returns></returns>
        [ErrorFilter("添加文件失败", ErrorFilterType.ReturnValue)]
        public ActionResult DoAddFolder()
        {
            Model.Logic.FileList file = new Model.Logic.FileList();
            TryUpdateModel(file);
            file.IsPrivate = Enums.BooleanType.No;
            file.UserId = GetCurrentUser.UserId;
            file.VerId = "";
            var rv = yq.GetService.Create<yq.IService.IFile>().AddFolder(file);

            return Json(rv);
        }


        /// <summary>
        /// 重命名或者移动
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("FileinfoId", VerifyHelp.FileAdminKey)]
        public ActionResult EditFolder()
        {
            var model = GetModel<Model.Logic.FileList>();
            yq.WebModel.File.FolderEdit info= new WebModel.File.FolderEdit();
            info.SetValue(model);

            return PartialView(info);
        }

        /// <summary>
        /// 重命名或者移动
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("FileinfoId", VerifyHelp.FileAdminKey)]
        public ActionResult DoEditFolder()
        {
            var model = GetModel<Model.Logic.FileList>();
            TryUpdateModel(model);
            var rv = yq.GetService.Create<yq.IService.IFile>().EditFile(model);

            return Json(rv);
        }

        /// <summary>
        /// 重新上传新的文件
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("FileinfoId", VerifyHelp.FileWriteKey, VerifyHelp.FileCheckKey)]
        public ActionResult EditFile()
        {
            var model = GetModel<Model.Logic.FileList>();

            return PartialView(model);
        }

        /// <summary>
        /// 重新上传新的文件
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("FileinfoId", VerifyHelp.FileWriteKey, VerifyHelp.FileCheckKey)]
        public ActionResult DoEditFile(string fileinfoId, string detailed)
        {
            Model.Logic.FileVer ver = new Model.Logic.FileVer()
            {
                CheckInUserId = GetCurrentUser.UserId,
                FileInfoId = fileinfoId,
                Detailed = detailed
            };
            var rv = yq.GetService.Create<yq.IService.IFile>().EditFileVer(fileinfoId, ver);

            return Json(rv);
        }


        /// <summary>
        /// 添加文件
        /// </summary>
        /// <returns></returns>
        public ActionResult AddFile()
        {
            Model.Logic.FileList file = new Model.Logic.FileList();

            return PartialView(file);
        }

        /// <summary>
        /// 执行添加文件
        /// </summary>
        /// <returns></returns>
        public ActionResult DoAddFlie(string fileId)
        {
            Model.Logic.FileList fileModel = new Model.Logic.FileList();
            TryUpdateModel(fileModel);
            fileModel.UserId = GetCurrentUser.UserId;
            var rv = yq.GetService.Create<yq.IService.IFile>().AddFile(fileModel, fileId);

            return Json(rv);
        }

        /// <summary>
        /// 签出文件
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("FileinfoId", VerifyHelp.FileWriteKey, VerifyHelp.FileCheckKey)]
        public ActionResult CheckOutFlie()
        {
            var model = GetModel<Model.Logic.FileList>();
            model.CheckTime = DateTime.Now;
            model.CheckUserId = GetCurrentUser.UserId;
            model.IsCheckOut = Enums.BooleanType.Yes;
            var rv = yq.GetService.Create<yq.IService.IFile>().EditFile(model);
            return Json(rv);
        }

        /// <summary>
        /// 签入文件
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("FileinfoId", VerifyHelp.FileWriteKey, VerifyHelp.FileCheckKey)]
        public ActionResult CheckInFlie()
        {
            var model = GetModel<Model.Logic.FileList>();
            model.IsCheckOut = Enums.BooleanType.No;
            var rv = yq.GetService.Create<yq.IService.IFile>().EditFile(model);

            return Json(rv);
        }

        /// <summary>
        /// 强制签入
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("FileinfoId", VerifyHelp.FileMyKey)]
        public ActionResult MyCheckInFlie()
        {
            Dictionary.ReturnValue<string> rv = new Dictionary.ReturnValue<string>();
            var model = GetModel<Model.Logic.FileList>();
            model.IsCheckOut = Enums.BooleanType.No;
            rv = yq.GetService.Create<yq.IService.IFile>().EditFile(model);

            return Json(rv);
        }

        /// <summary>
        /// 删除文件夹和文件夹
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("FileinfoId", VerifyHelp.FileMyKey, VerifyHelp.FileCheckKey)]
        public ActionResult DoDelFlie(string fileinfoId)
        {
            var fileModel = this.GetModel<yq.Model.Logic.FileList>();
            yq.Dictionary.ReturnValue<string> rv = new Dictionary.ReturnValue<string>();

            if (fileModel.Type == Enums.FileList.Type.File)
            {
                //文件删除到回收站
                rv = yq.GetService.Create<yq.IService.IFile>().RecoverFlie(fileinfoId);
            }
            else
            {
                //文件夹直接删除
                rv = yq.GetService.Create<yq.IService.IFile>().DelFlie(fileinfoId);
            }

            return Json(rv);
        }

        /// <summary>
        /// 上传
        /// </summary>
        /// <returns></returns>
        public ActionResult UploadFlie(string superiorId)
        {
            var model = GetModel<Model.Logic.FileList>();
            model.SuperiorId = superiorId;

            return PartialView();
        }

        /// <summary>
        /// 上传
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("SuperiorId", VerifyHelp.FileMyKey)]
        public ActionResult DoUploadFlie(string fileId)
        {
            Model.Logic.FileList model = new Model.Logic.FileList();
            TryUpdateModel(model);
            var rv = yq.GetService.Create<yq.IService.IFile>().AddFile(model, fileId);

            return Json(rv);
        }


        /// <summary>
        /// 左边导航
        /// </summary>
        /// <returns></returns>
        public ActionResult Navigation()
        {
            return PartialView();
        }


        /// <summary>
        /// 查询文件夹
        /// </summary>
        /// <returns></returns>
        public ActionResult FolderTree()
        {
            return PartialView();
        }

        /// <summary>
        /// 查询文件夹
        /// </summary>
        /// <returns></returns>
        public ActionResult DoFolderTree()
        {
            Search.File.FileListSeek search = new Search.File.FileListSeek()
            {
                Status = Enums.FileList.Status.Normal,
                Type = Enums.FileList.Type.Folder,
                UserId = GetCurrentUser.UserId
            };
            var list = yq.GetService.Create<yq.IService.IFile>().SelectFile(search); ;
            zTreeExt zlist = new zTreeExt();
            zlist.Add("-1", "", "主目录", true);
            foreach (var info in list)
            {
                zlist.Add(info.FileinfoId, info.SuperiorId, info.Ttile, false);
            }

            return Json(zlist.List);
        }
    }
}
